package com.zzl.car.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.car.entity.CarDetailInfo;
import com.zzl.car.entity.CarOrder;
import com.zzl.car.entity.Insurance;
import com.zzl.car.entity.User;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.mapper.ZzwInsuranceMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ZzwInsuranceService {

    @Autowired
    private ZzwInsuranceMapper insuranceMapper;

    // 得到保险服务全部信息
    public PageInfo<Insurance> getAllInsurance(Integer currentPage){
        int pageAmount = 5;
        PageInfo<Insurance> pageInfo = null;
        List<Insurance> insurances = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        insurances = insuranceMapper.getAllInsurance();

        pageInfo = new PageInfo<>(insurances);
        pageInfo.setList(insurances);

        return pageInfo;
    }

    //获取用户信息
    public PageInfo<User> getUserBaseInfo(String name,Integer gender,Integer currentPage){
        int pageAmount = 10;
        PageInfo<User> pageInfo = null;
        List<User> user = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        user = insuranceMapper.getUserBaseInfo(name,gender);

        pageInfo = new PageInfo<>(user);
        pageInfo.setList(user);

        return pageInfo;
    }

    // 条件查询 保险服务
    public PageInfo<Insurance> searchInsurance(String name,Integer state,Integer currentPage){
        int pageAmount = 5;
        PageInfo<Insurance> pageInfo = null;
        List<Insurance> insurances = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        insurances = insuranceMapper.searchInsurance(name,state);

        pageInfo = new PageInfo<>(insurances);
        pageInfo.setList(insurances);

        return pageInfo;
    }

    // 删除服务保障信息
    public String deleteInsurance(Integer id) {
        if(insuranceMapper.getOrderByInsuranceId(id).size() == 0){
            if(insuranceMapper.deleteInsurance(id) > 0)
                return "1";
            else
                throw new ZdpException(2949,"未知原因，修改失败");
    }
        throw new ZdpException(2950,"修改失败，租赁订单中包含该服务，无法删除");
    }

    // 添加保障服务信息
    public String addInsurance(Insurance in){
        int id = Integer.valueOf(String.valueOf(System.currentTimeMillis()).substring(6,9));
        in.setId(id);

        List<Insurance> l =insuranceMapper.getAllInsurance();
        for (Insurance ins:l) {
            if(ins.getName() == in.getName() || ins.getContent() == in.getContent()){
                throw new ZdpException(2990,"存在同样的名称或内容，添加失败");
            }
        }

        if(insuranceMapper.addInsurance(in) <= 0)
            throw new ZdpException(2990,"添加失败");

        return "1";
    }

    // 修改保障服务信息
    public String updateInsurance(Insurance in){
        if(insuranceMapper.updateInsurance(in) <= 0)
            throw new ZdpException(2991,"修改失败");
        return "1";
    }
}
